package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YunShangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "ysll")
public class YunShangAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(YunShangAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
	
		String productId = orderBean.getProductCode();//param.getString("pkgNo");
		String phoneNo = orderBean.getPhoneNo();//param.getString("phoneNo");
		String cstmOrderNo = orderBean.getIds();//param.getString("transNo");
		log.info("between- 云上流量流量订购请求productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = YunShangServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");
	

		if (("00".equals(code))) {

			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("充值成功");
			orderBean.setStatus("7");
		} else if (("02".equals(code))) {
			
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");

		} else {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
	
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();

		String result = YunShangServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between -云上流量流量订购状态查询接口请求参数 ：cstmOrderNo" + cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");

		if (("00".equals(code))) {
	
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("充值成功");
			orderBean.setStatus("7");
		} else if (("02".equals(code))) {

			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订单充值中");
			orderBean.setStatus("1");

		} else {
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("充值失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
